package com.qin.mall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qin.mall.domain.*;
import com.qin.mall.dao.UmsRoleDao;
import com.qin.mall.mapper.UmsRoleMenuRelationMapper;
import com.qin.mall.mapper.UmsRoleResourceRelationMapper;
import com.qin.mall.service.UmsResourceService;
import com.qin.mall.service.UmsRoleService;
import com.qin.mall.mapper.UmsRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author 覃剑
* @description 针对表【ums_role(后台用户角色表)】的数据库操作Service实现
* @createDate 2022-11-11 10:15:47
*/
@Service
public class UmsRoleServiceImpl extends ServiceImpl<UmsRoleMapper, UmsRole>
    implements UmsRoleService{
    
    @Autowired
    private UmsRoleDao roleDao;
    @Autowired
    private UmsRoleMapper roleMapper;
    @Autowired
    private UmsRoleMenuRelationMapper roleMenuRelationMapper;
    @Autowired
    private UmsRoleResourceRelationMapper roleResourceRelationMapper;
    @Autowired
    private UmsResourceService resourceService;
    
    
    /**
     * 添加角色
     */
    @Override
    public int create(UmsRole role) {
        role.setCreateTime(new Date());
        role.setAdminCount(0);
        role.setSort(0);
        int count = roleMapper.insert(role);
        return count;
    }

    /**
     * 修改角色信息
     */
    @Override
    public int update(Long id, UmsRole role) {
        role.setId(id);
        int count = roleMapper.updateById(role);
        return count;
    }

    /**
     * 批量删除角色
     */
    @Override
    public int delete(List<Long> ids) {
        int count = roleMapper.deleteBatchIds(ids);
        return count;
    }

    /**
     * 获取所有角色列表
     */
    @Override
    public List<UmsRole> listRole() {
        List<UmsRole> list = roleMapper.selectList(null);
        return list;
    }

    /**
     * 分页获取角色列表
     */
    @Override
    public Page<UmsRole> list(String keyword, Integer pageSize, Integer pageNum) {
        Page<UmsRole> page = new Page<>(pageNum, pageSize);
        QueryWrapper<UmsRole> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(keyword)){
            wrapper.like("name",keyword);
        }
        Page<UmsRole> selectPage = roleMapper.selectPage(page, wrapper);
        return selectPage;
    }

    /**
     * 根据管理员ID获取对应菜单
     */
    @Override
    public List<UmsMenu> getMenuList(Long adminId) {
        return roleDao.getMenuList(adminId);
    }

    /**
     * 获取角色相关资源
     */
    @Override
    public List<UmsResource> listResource(Long roleId) {
        return roleDao.getResourceListByRoleId(roleId);
    }

    /**
     * 给角色分配菜单
     */
    @Override
    public int allocMenu(Long roleId, List<Long> menuIds) {
        //先删除原有关系
        QueryWrapper<UmsRoleMenuRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        int count = roleMenuRelationMapper.delete(wrapper);
        //批量插入新关系
        for (Long menuId : menuIds) {
            UmsRoleMenuRelation relation = new UmsRoleMenuRelation();
            relation.setRoleId(roleId);
            relation.setMenuId(menuId);
            roleMenuRelationMapper.insert(relation);
        }
        return menuIds.size();
    }

    /**
     * 给角色分配资源
     */
    @Override
    public int allocResource(Long roleId, List<Long> resourceIds) {
        //先删除原有关系
        QueryWrapper<UmsRoleResourceRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        int count = roleResourceRelationMapper.delete(wrapper);
        //批量插入新关系
        for (Long resourceId : resourceIds) {
            UmsRoleResourceRelation relation = new UmsRoleResourceRelation();
            relation.setRoleId(roleId);
            relation.setResourceId(resourceId);
            roleResourceRelationMapper.insert(relation);
        }
        resourceService.initResourceRolesMap();
        return resourceIds.size();
    }

    /**
     * 获取角色相关菜单
     */
    @Override
    public List<UmsMenu> listMenu(Long roleId) {
        return roleDao.getMenuListByRoleId(roleId);
    }
}




